package com.app.controller.article;

import com.app.aop.has.HasRedissonLock;
import com.app.aop.has.HasRole;
import com.app.domain.bo.article.TagBo;
import com.app.domain.page.Page;
import com.app.domain.po.article.TagPo;
import com.app.domain.vo.article.TagVo;
import com.app.framework.common.R;
import com.app.service.article.TagService;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签控制器
 */
@RestController
@RequestMapping("/api/tag")
public class TagController {

    @Resource
    private TagService tagService;

    /**
     * 标签列表
     *
     * @param bo 入参
     */
    @PostMapping("/list")
    public R<Page<TagVo>> lists(@RequestBody TagBo bo) {
        return R.ok(tagService.lists(bo));
    }

    /**
     * 添加标签 - 单个
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("save_tag")
    @PostMapping("/save")
    public R<?> saveOne(@RequestBody TagBo bo) {
        tagService.saveOne(bo);
        return R.ok(null, "添加成功");
    }

    /**
     * 批量添加标签
     *
     * @param names 标签名称
     */
    @HasRole()
    @HasRedissonLock("save_tags")
    @PostMapping("/batchSave")
    public R<?> batchSave(@RequestBody List<String> names) {
        tagService.batchSave(names);
        return R.ok(null, "添加成功");
    }

    /**
     * 修改标签
     *
     * @param bo 入参
     */
    @HasRole()
    @HasRedissonLock("update_tag")
    @PostMapping("/update")
    public R<?> updateOne(@RequestBody TagBo bo) {
        Assert.notNull(bo.getId(), "ID不能为空");
        tagService.updateOne(bo);
        return R.ok(null, "修改成功");
    }

    /**
     * 标签详情
     *
     * @param tagId 标签ID
     */
    @GetMapping("/details/{tagId}")
    public R<TagPo> details(@PathVariable("tagId") Long tagId) {
        return R.ok(tagService.details(tagId));
    }

    /**
     * 删除标签
     *
     * @param tagId 标签ID
     */
    @HasRole()
    @HasRedissonLock("delete_tag")
    @PostMapping("/delete/{tagId}")
    public R<?> deleteOne(@PathVariable("tagId") Long tagId) {
        tagService.deleteOne(tagId);
        return R.ok(null, "删除成功");
    }

}